package domotica.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

import domotica.housemanager.HouseManagerImpl;
import domotica.housemanager.HouseManagerImplService;
import domotica.houseowner.HouseOwner;
import domotica.houseowner.HouseOwnerService;

public class DomoticaLoginFormGUI extends JFrame implements ActionListener, KeyListener{
	JButton connectButton;
	JPanel upperPanel;
	JPanel lowerPanel;
	JLabel userLabel,pswLabel;
	final JTextField  userText,pswText;

	public DomoticaLoginFormGUI()
	{
		this.setIconImage(DomoticaGUI.createImage("images/logoframe1.gif"));
		this.setLocationByPlatform(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Domotica Login");
		this.setResizable(false);
		
		userLabel = new JLabel();
		userLabel.setText("Username:");
		userText = new JTextField(15);

		pswLabel = new JLabel();
		pswLabel.setText("Password:");
		pswText = new JPasswordField(15);
		
		ImageIcon middleButtonIcon = DomoticaGUI.createImageIcon("images/connect.png");
		connectButton=new JButton(middleButtonIcon);
		connectButton.setBackground(new Color(103,131,157));

		upperPanel=new JPanel(new GridLayout(2,2));
		upperPanel.add(userLabel);
		upperPanel.add(userText);
		upperPanel.add(pswLabel);
		upperPanel.add(pswText);
		
		lowerPanel=new JPanel(new FlowLayout());

		lowerPanel.add(connectButton);
		
		add(upperPanel,BorderLayout.CENTER);
		add(lowerPanel,BorderLayout.SOUTH);
		
		connectButton.addActionListener(this);
		connectButton.addKeyListener(this);
		userText.addKeyListener(this);
		pswText.addKeyListener(this);
		
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		String user=userText.getText();
		String password=pswText.getText();


		try {

			HouseOwner houseOwner = this.login(user, password);

			this.dispose();
			DomoticaGUI gui = new DomoticaGUI(houseOwner, 700, 500);
			gui.setVisible(true);

		} catch (Exception e){
			JOptionPane.showMessageDialog(this,e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
		}

	}

	public HouseOwner login(String user, String password) {
		
		HouseManagerImplService houseManagerWS = new HouseManagerImplService();
		HouseManagerImpl houseManager =  houseManagerWS.getPort(HouseManagerImpl.class);
		W3CEndpointReference ref=houseManager.login(user, password);
		HouseOwnerService houseOwnerWS = new HouseOwnerService();
		HouseOwner houseOwner = houseOwnerWS.getPort(ref, HouseOwner.class);
		
		return houseOwner;
		
	}

	public static void main(String arg[])
	{
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (UnsupportedLookAndFeelException e) {
			// utilizziamo il default lnf
		} catch (ClassNotFoundException e) {
			// utilizziamo il default lnf
		} catch (InstantiationException e) {
			// utilizziamo il default lnf
		} catch (IllegalAccessException e) {
			// utilizziamo il default lnf
		}

		try
		{
			DomoticaLoginFormGUI loginForm=new DomoticaLoginFormGUI();
			loginForm.setSize(270,140);
			loginForm.setVisible(true);
		}
		catch(Exception e)
		{JOptionPane.showMessageDialog(null, e.getMessage());}
	}

	public void keyPressed(KeyEvent keyEvent) {
		if (keyEvent.getKeyCode()==KeyEvent.VK_ENTER){
			connectButton.doClick();
		}
	}

	public void keyReleased(KeyEvent arg0) {}

	public void keyTyped(KeyEvent arg0) {}
}
